<?php


namespace app\common\service;

use app\common\service\Base;
use think\Db;


/**
 * 日报
 * @author zhuguosen 905502314@qq.com
 * @date 2022/3/8
 * @package app\common\service
 */
class Daily extends Base
{

    public $temp = [];

    /**
     * 上月下单出货情况
     * @return array
     * @author zhuguosen 905502314@qq.com
     * @date 2022/3/8
     */
    public function chart_order_delivery($param)
    {
        //获取参数，并处理
        $param = $this->_statistics_getParam($param);
        //查询数据
        $data = $this->_statistics_searchData($param);
        //处理数据 以适应图表
        $highcharts_data = $this->_statistics_dealWith($param, $data);
        return $highcharts_data;
    }

    private function _statistics_getParam($param)
    {
        $time_slot = $this->time_slot(true);
        switch ($param['date_type']) {
            case 'last_month'://上月
                $param['start_time'] = strtotime($time_slot['lastmonth']['begin']);
                $param['end_time'] = strtotime($time_slot['lastmonth']['end']);
                $param['start'] = date('Y-m-d H:i:s', $param['start_time']);
                $param['end'] = date('Y-m-d H:i:s', $param['end_time']);
                break;
            case '10_days'://最近10天
                $param['start_time'] = strtotime(date('Y-m-d 00:00:00', time() - 9 * 24 * 3600));
                $param['end_time'] = time();
                $param['start'] = date('Y-m-d H:i:s', $param['start_time']);
                $param['end'] = date('Y-m-d H:i:s', $param['end_time']);
                break;
            case 'this_month'://本月
                $param['start_time'] = strtotime($time_slot['thismonth']['begin']);
                $param['end_time'] = strtotime($time_slot['thismonth']['end']);
                $param['start'] = date('Y-m-d H:i:s', $param['start_time']);
                $param['end'] = date('Y-m-d H:i:s', $param['end_time']);
                break;
            case '31_days':
                $param['start_time'] = mktime(0, 0, 0, date('m'), date('d') - 31, date('Y'));
                $param['end_time'] = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
                $param['start'] = date('Y-m-d H:i:s', $param['start_time']);
                $param['end'] = date('Y-m-d H:i:s', $param['end_time']);
                break;
        }
        return $param;
    }

    private function _statistics_searchData($param)
    {
        $sql_where = $sql_where2 = '';
        if ($param['is_auto_import'] > 0) {
            $sql_where .= " and o.is_auto_import={$param['is_auto_import']} ";
            $sql_where2 .= " and bc.is_auto_import={$param['is_auto_import']} ";
        }
        if ($param['factory_id'] > 0) {
            $sql_where .= " and o.factory_id={$param['factory_id']} ";
            $sql_where2 .= " and bc.factory_id={$param['factory_id']} ";
        }
        $result = [];
        switch ($param['unit']) {
            case 'day':
                $sql_sku_type = "";
                switch ($param['sku_type']) {
                    case '针织':
                        $sql_sku_type = "and sku.sku_type='针织'";
                        break;
                    case '梭织':
                        $sql_sku_type = "and sku.sku_type='梭织'";
                        break;
                }
                switch ($param['type']) {
                    case '金额':
                        //出货总金额 加工费
                        $sql = "SELECT    date_format(FROM_UNIXTIME(od.delivery_time),'%y年%m月%d日') as sdate
,sum(IFNULL(od.real_shipment_count,0)*o.order_price) as num
,sum(IFNULL(od.real_shipment_count,0)*IFNULL(oext.cost_profit,0)) as num2
FROM wx_order_delivery od 
LEFT JOIN wx_order o on (o.id=od.order_id)
LEFT JOIN wx_order_ext oext on (o.id=oext.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2) {$sql_sku_type} {$sql_where} and od.delivery_status not in (6,7)
and od.delivery_time >= {$param['start_time']} and od.delivery_time <={$param['end_time']}
group by  sdate
ORDER BY od.delivery_time asc  ";
                        $money_all = db()->query($sql);
                        $money_chu_zhong = $money_all;//出货总金额
                        $money_chu_jia = [];//出货加工费
                        foreach ($money_all as $k => $v) {
                            $money_chu_jia[] = [
                                'sdate' => $v['sdate'],
                                'num' => $v['num2'],
                            ];
                        }
                        $money_chu_zhong = $this->_date_bu($param, 'jian_day_0', $money_chu_zhong);
                        $money_chu_jia = $this->_date_bu($param, 'jian_day_0', $money_chu_jia);
                        //下单加工费
                        $sql = "SELECT    date_format(FROM_UNIXTIME(o.create_time),'%y年%m月%d日') as sdate
,sum(o.quantity*IFNULL(oext.cost_profit,0)) as num
FROM wx_order o
LEFT JOIN wx_order_ext oext on (o.id=oext.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2)  {$sql_sku_type} {$sql_where}
and o.create_time >= {$param['start_time']} and o.create_time <={$param['end_time']}
group by  sdate
ORDER BY o.create_time asc  ";
                        $money_order_jia = db()->query($sql);
                        $money_order_jia = $this->_date_bu($param, 'jian_day_0', $money_order_jia);
                        return [
                            'money_chu_zhong' => $money_chu_zhong,
                            'money_chu_jia' => $money_chu_jia,
                            'money_order_jia' => $money_order_jia,
                        ];
                        break;
                    case '件数':
                        //下单件数
                        $sql = "SELECT    date_format(FROM_UNIXTIME(o.create_time),'%y年%m月%d日') as sdate
,sum(o.quantity) as num
FROM wx_order o
LEFT JOIN wx_order_ext oext on (o.id=oext.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2)  {$sql_sku_type} {$sql_where}
and o.create_time >= {$param['start_time']} and o.create_time <={$param['end_time']}
group by  sdate
ORDER BY o.create_time asc ";
                        $jian_order = db()->query($sql);
                        $jian_order = $this->_date_bu($param, 'jian_day_0', $jian_order);
                        //出货件数
                        $sql = "SELECT    date_format(FROM_UNIXTIME(od.delivery_time),'%y年%m月%d日') as sdate
,sum(od.real_shipment_count) as num
FROM wx_order_delivery od 
LEFT JOIN wx_order o on (o.id=od.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2) {$sql_sku_type} {$sql_where} and od.delivery_status not in (6,7)
and od.delivery_time >= {$param['start_time']} and od.delivery_time <={$param['end_time']}
group by  sdate
ORDER BY od.delivery_time asc  ";
                        $jian_delivery = db()->query($sql);
                        $jian_delivery = $this->_date_bu($param, 'jian_day_0', $jian_delivery);
                        //日均销量
                        $sql = "SELECT   date_format(bc.confirm_date,'%y年%m月%d日') as sdate 
,sum(bc.average_daily_sales_num) as num
FROM wx_break_code bc
LEFT JOIN wx_factory f ON (bc.factory_id=f.id)
LEFT JOIN wx_sku sku on (sku.sku=bc.sku)
where 1=1  {$sql_sku_type} {$sql_where2}
and bc.confirm_time >= {$param['start_time']} and bc.confirm_time <={$param['end_time']}
group by  sdate
order by bc.confirm_time asc
";
                        $average_daily_sales_num = db()->query($sql);
                        $average_daily_sales_num = $this->_date_bu($param, 'jian_day_0', $average_daily_sales_num);
                        return [
                            'jian_order' => $jian_order,
                            'jian_delivery' => $jian_delivery,
                            'average_daily_sales_num' => $average_daily_sales_num,
                        ];
                        break;
                    case '订单个数':
                        //下单个数
                        $sql = "SELECT    date_format(FROM_UNIXTIME(o.create_time),'%y年%m月%d日') as sdate
,count(o.id) as num
FROM wx_order o
LEFT JOIN wx_order_ext oext on (o.id=oext.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2)  {$sql_sku_type} {$sql_where}
and o.create_time >= {$param['start_time']} and o.create_time <={$param['end_time']}
group by  sdate
ORDER BY o.create_time asc  ";
                        $ge_order = db()->query($sql);
                        $ge_order = $this->_date_bu($param, 'jian_day_0', $ge_order);
                        //出单个数
                        $sql = "SELECT   date_format(FROM_UNIXTIME(od.delivery_time),'%y年%m月%d日') as sdate
,count(distinct  od.order_id) as num
FROM wx_order_delivery od 
LEFT JOIN wx_order o on (o.id=od.order_id)
LEFT JOIN wx_sku sku on (sku.sku=o.sku)
where 
o.status in (0,1,2)  {$sql_sku_type} {$sql_where} and od.delivery_status not in (6,7)
and od.delivery_time >= {$param['start_time']} and od.delivery_time <={$param['end_time']}
group by  sdate
ORDER BY od.delivery_time asc  ";
                        $ge_chu_order = db()->query($sql);
                        $ge_chu_order = $this->_date_bu($param, 'jian_day_0', $ge_chu_order);
                        return [
                            'ge_order' => $ge_order,
                            'ge_chu_order' => $ge_chu_order,
                        ];
                        break;
                }
                break;
        }
        return $result;
    }

    private function _date_bu($param, $type = 'day_0', $data = [])
    {
        switch ($type) {
            case 'jian_day_0':
                $sql = "SELECT  date_format(date,'%y年%m月%d日') as sdate 
FROM wx_date 
where time >= {$param['start_time']} and time <={$param['end_time']}
order by id asc
";
                $date_array = db()->query($sql);
                $res = [];
                foreach ($date_array as $k => $v) {
                    $temp = [
                        'sdate' => $v['sdate'],
                        'num' => 0,
                    ];
                    foreach ($data as $k3 => $v3) {
                        if ($v3['sdate'] == $v['sdate']) {
                            $temp['num'] = $v3['num'];
                            break;
                        }
                    }
                    $res[] = $temp;
                }
                return $res;
                break;
        }
    }

    private function _statistics_dealWith($param = [], $data = [])
    {
        $chart = [
//            'chart' => [
//                'type' => 'line',
//            ],
            'title' => [
                'text' => '月平均气温23',
            ],
            'subtitle' => [
                'text' => '数据来源: WorldClimate.com',
            ],
            'xAxis' => [
                'categories' => ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                //标示线
//                'plotLines' => [[
//                    'color' => 'red',            //线的颜色，定义为红色
//                    'dashStyle' => 'longdashdot',//标示线的样式，默认是solid（实线），这里定义为长虚线
//                    'value' => 3,                //定义在哪个值上显示标示线，这里是在x轴上刻度为3的值处垂直化一条线
//                    'width' => 2                 //标示线的宽度，2px
//                ]],
            ],
            'yAxis' => [
                'title' => [
                    'text' => ''
                ],
            ],
            'plotOptions' => [
                'series' => [
                    'label' => [
                        'connectorAllowed' => false
                    ],
//                    'pointStart' => 2010,
                ],
                'line' => [
                    'dataLabels' => [
                        'enabled' => true,// 开启数据标签
                    ],
//                    'enableMouseTracking' => false, // 关闭鼠标跟踪，对应的提示框、点击事件会失效
                ],
            ],
            'series' => [
                [
                    'name' => '东京',
                    'data' => [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],
                ],
                [
                    'name' => '伦敦',
                    'data' => [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8],
                ],
            ],
            'credits' => [
                'enabled' => false,//版权信息 关闭
            ],
            'legend' => [
                'layout' => 'vertical',
                'align' => 'right',
                'verticalAlign' => 'middle',
            ],
            'tooltip' => [
                'pointFormatter' => "",
            ]
        ];
        $chart['title']['text'] = "";
        $chart['subtitle']['text'] = "";
        $fisrt_key = '';
        foreach ($data as $k => $v) {
            $fisrt_key = $k;
            break;
        }
        $chart['xAxis']['categories'] = $this->_get_xAxis_categories($data[$fisrt_key]);
        $chart['series'] = $this->_get_series($data, $chart['xAxis']['categories']);
        //去除年的信息 $chart['xAxis']['categories']   22年01月01日  变为 01月01日
        foreach ($chart['xAxis']['categories'] as $k => $v) {
            $chart['xAxis']['categories'][$k] = substr($v, 5, 1000);
            $chart['xAxis']['categories'][$k] = str_replace('月','-',$chart['xAxis']['categories'][$k]);
        }
        return [
            'chart' => $chart,
        ];
    }

    private function _get_xAxis_categories($data)
    {
        $xAxis_categories = [];
        foreach ($data as $k => $v) {
            $xAxis_categories[$v['sdate']] = 0;
        }
        $xAxis_categories_res = [];
        foreach ($xAxis_categories as $k => $v) {
            $xAxis_categories_res[] = $k;
        }
        return $xAxis_categories_res;
    }

    private function _get_series($data, $categories)
    {
        $res = [];
        $f_list = [];
        foreach ($data as $k => $v) {
            foreach ($v as $k2 => $v2) {
                $name = '';
                switch ($k) {
                    case 'jian_order':
                        $name = '下单件数';
                        break;
                    case 'jian_delivery':
                        $name = '出货件数';
                        break;
                    case 'average_daily_sales_num':
                        $name = '日均销量';
                        break;
                    case 'ge_order':
                        $name = '下单个数';
                        break;
                    case 'ge_chu_order':
                        $name = '出单个数';
                        break;
                    case 'money_chu_zhong':
                        $name = '出货总金额';
                        break;
                    case 'money_chu_jia':
                        $name = '出货加工费';
                        break;
                    case 'money_order_jia':
                        $name = '下单加工费';
                        break;
                }
                $f_list[$name][] = [
                    'sdate' => $v2['sdate'],
                    'num' => $v2['num'],
                ];
            }
        }
        foreach ($f_list as $k => $v) {
            $temp = [
                'name' => $k,
                'data' => [],
            ];
            foreach ($categories as $k2 => $v2) {
                $flag = 0;
                $num = 0;
                foreach ($v as $k3 => $v3) {
                    if ($v2 == $v3['sdate']) {
                        $num = $v3['num'];
                        $flag = 1;
                        break;
                    }
                }
                $temp['data'][] = floatval($num);//必须是数值，不能字符串
            }
            $res[] = $temp;
        }
        return $res;
    }

    /**
     * 时间段（本日，昨日，本周，上周，本月，上月）
     * @param bool $date_flag false--以秒为单的时间戳  true日期
     * @param string $auto_start 2022-01-01
     * @param string $auto_end 2022-01-10
     * @param int $type 扩展使用--作用auto的开始与结束时间
     * @return array
     * @author zhuguosen 905502314@qq.com
     * @date 2022/3/16
     */
    public function time_slot($date_flag = false, $auto_start = '', $auto_end = '', $type = 0)
    {
        $time = [];
        $time['today']['begin'] = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $time['today']['end'] = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        $time['yesterday']['begin'] = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $time['yesterday']['end'] = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        $time['thisweek']['begin'] = mktime(0, 0, 0, date("m"), date("d") - date("w") + 1, date("Y"));
        $time['thisweek']['end'] = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));
        $time['lastweek']['begin'] = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y'));
        $time['lastweek']['end'] = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y'));
        $time['thismonth']['begin'] = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $time['thismonth']['end'] = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $time['lastmonth']['begin'] = mktime(0, 0, 0, date("m") - 1, 1, date("Y"));
        $time['lastmonth']['end'] = mktime(23, 59, 59, date("m"), 0, date("Y"));
        switch ($type) {
            case 0:
            default:
                $time['auto']['begin'] = strtotime(date('Y-m-d', strtotime($auto_start)));
                $time['auto']['end'] = strtotime(date('Y-m-d 23:59:59', strtotime($auto_end)));
                break;
        }
        if ($date_flag == true) {
            foreach ($time as $k => $v) {
                $time[$k]['begin'] = date('Y-m-d H:i:s', $v['begin']);
                $time[$k]['end'] = date('Y-m-d H:i:s', $v['end']);
            }
        }
        return $time;
    }

    public function chart_ddjd($param)
    {
        //获取参数，并处理
        $param = $this->_ddjd_getParam($param);
        //查询数据
        $data = $this->_ddjd_searchData($param);
        //处理数据 以适应图表
        $highcharts_data = $this->_ddjd_dealWith($param, $data);
        return $highcharts_data;
    }

    private function _ddjd_getParam($param)
    {
        switch ($param['date_type']) {
            case '7_days'://最近7天
                $param['start_time'] = strtotime(date('Y-m-d 00:00:00', time() - 7 * 24 * 3600));
                $param['end_time'] = time();
                $param['start'] = date('Y-m-d H:i:s', $param['start_time']);
                $param['end'] = date('Y-m-d H:i:s', $param['end_time']);
                break;
        }
        return $param;
    }

    private function _ddjd_searchData($param)
    {
        $sql_where = '';
        if ($param['is_auto_import'] > 0) {
            $sql_where .= " and o.is_auto_import={$param['is_auto_import']} ";
        }
        if ($param['factory_id'] > 0) {
            $sql_where .= " and o.factory_id={$param['factory_id']} ";
        }
        $result = [];
        switch ($param['unit']) {
            case 'day':
                switch ($param['type']) {
                    case 'caigou':
                        $sql = "SELECT  date_format(from_unixtime(t0.caigou_confirm_time_min),'%Y年%m月%d日') as sdate 
,count(t0.order_id) as num_order
,sum(quantity) as num
FROM 
(
SELECT   om.order_id,o.client_ordernum
,max(om.caigou_confirm_status) as caigou_confirm_status_max 
,min(om.caigou_confirm_status) as caigou_confirm_status_min
,max(om.caichuang_can_do) as caichuang_can_do_max
,min(om.caichuang_can_do) as caichuang_can_do_min
,o.caichuang_can_do,o.caichuang_status,o.chejian_status,o.tail_status
,min(om.caigou_confirm_time) as caigou_confirm_time_min
,o.caichuang_can_do_time,o.caichuang_over_time,o.chejian_over_time,o.tail_over_time
,o.quantity
FROM wx_order_material om
LEFT JOIN wx_order o on (om.order_id=o.id)
LEFT JOIN wx_material m on (om.material_id=m.id)
WHERE o.status in (0,1) and o.client_status <> 99 {$sql_where}
and m.type <> 'accessories' 
GROUP BY om.order_id
)  t0
WHERE 1=1
and t0.caigou_confirm_time_min >= {$param['start_time']}
and t0.caigou_confirm_time_min <= {$param['end_time']}
GROUP BY sdate";
                        $list = db()->query($sql);
                        $this->temp['caigou'] = $list;
                        $caigou = $this->_ddjd_date_bu($param, 'day_0', $list);
                        return [
                            'caigou' => $caigou,
                        ];
                        break;
                    case 'mianliao' :
                        $sql = "SELECT  date_format(from_unixtime(t0.caichuang_can_do_time),'%Y年%m月%d日') as sdate 
,count(t0.order_id) as num_order
,sum(quantity) as num
FROM 
(
SELECT   om.order_id,o.client_ordernum
,max(om.caigou_confirm_status) as caigou_confirm_status_max 
,min(om.caigou_confirm_status) as caigou_confirm_status_min
,max(om.caichuang_can_do) as caichuang_can_do_max
,min(om.caichuang_can_do) as caichuang_can_do_min
,o.caichuang_can_do,o.caichuang_status,o.chejian_status,o.tail_status
,min(om.caigou_confirm_time) as caigou_confirm_time_min
,o.caichuang_can_do_time,o.caichuang_over_time,o.chejian_over_time,o.tail_over_time
,o.quantity
FROM wx_order_material om
LEFT JOIN wx_order o on (om.order_id=o.id)
LEFT JOIN wx_material m on (om.material_id=m.id)
WHERE o.status in (0,1) and o.client_status <> 99 {$sql_where}
and m.type <> 'accessories' 
GROUP BY om.order_id
)  t0
WHERE 1=1
and t0.caichuang_can_do_time >= {$param['start_time']}
and t0.caichuang_can_do_time <= {$param['end_time']}
GROUP BY sdate";
                        $list = db()->query($sql);
                        $this->temp['mianliao'] = $list;
                        $mianliao = $this->_ddjd_date_bu($param, 'day_0', $list);
                        return [
                            'mianliao' => $mianliao,
                        ];
                        break;
                    case 'caijian':
                        $sql = "SELECT  date_format(from_unixtime(t0.caichuang_over_time),'%Y年%m月%d日') as sdate 
,count(t0.order_id) as num_order
,sum(quantity) as num
FROM 
(
SELECT   om.order_id,o.client_ordernum
,max(om.caigou_confirm_status) as caigou_confirm_status_max 
,min(om.caigou_confirm_status) as caigou_confirm_status_min
,max(om.caichuang_can_do) as caichuang_can_do_max
,min(om.caichuang_can_do) as caichuang_can_do_min
,o.caichuang_can_do,o.caichuang_status,o.chejian_status,o.tail_status
,min(om.caigou_confirm_time) as caigou_confirm_time_min
,o.caichuang_can_do_time,o.caichuang_over_time,o.chejian_over_time,o.tail_over_time
,o.quantity
FROM wx_order_material om
LEFT JOIN wx_order o on (om.order_id=o.id)
LEFT JOIN wx_material m on (om.material_id=m.id)
WHERE o.status in (0,1) and o.client_status <> 99 {$sql_where}
and m.type <> 'accessories' 
GROUP BY om.order_id
)  t0
WHERE 1=1
and t0.caichuang_over_time >= {$param['start_time']}
and t0.caichuang_over_time <= {$param['end_time']}
GROUP BY sdate";
                        $list = db()->query($sql);
                        $this->temp['caijian'] = $list;
                        $mianliao = $this->_ddjd_date_bu($param, 'day_0', $list);
                        return [
                            'caijian' => $mianliao,
                        ];
                        break;
                    case 'chefen':
                        $sql = "SELECT  date_format(from_unixtime(t0.chejian_over_time),'%Y年%m月%d日') as sdate 
,count(t0.order_id) as num_order
,sum(quantity) as num
FROM 
(
SELECT   om.order_id,o.client_ordernum
,max(om.caigou_confirm_status) as caigou_confirm_status_max 
,min(om.caigou_confirm_status) as caigou_confirm_status_min
,max(om.caichuang_can_do) as caichuang_can_do_max
,min(om.caichuang_can_do) as caichuang_can_do_min
,o.caichuang_can_do,o.caichuang_status,o.chejian_status,o.tail_status
,min(om.caigou_confirm_time) as caigou_confirm_time_min
,o.caichuang_can_do_time,o.caichuang_over_time,o.chejian_over_time,o.tail_over_time
,o.quantity
FROM wx_order_material om
LEFT JOIN wx_order o on (om.order_id=o.id)
LEFT JOIN wx_material m on (om.material_id=m.id)
WHERE o.status in (0,1) and o.client_status <> 99 {$sql_where}
and m.type <> 'accessories' 
GROUP BY om.order_id
)  t0
WHERE 1=1
and t0.chejian_over_time >= {$param['start_time']}
and t0.chejian_over_time <= {$param['end_time']}
GROUP BY sdate";
                        $list = db()->query($sql);
                        $this->temp['chefen'] = $list;
                        $mianliao = $this->_ddjd_date_bu($param, 'day_0', $list);
                        return [
                            'chefen' => $mianliao,
                        ];
                        break;
                    case 'tail':
                        $sql = "SELECT  date_format(from_unixtime(t0.tail_over_time),'%Y年%m月%d日') as sdate 
,count(t0.order_id) as num_order
,sum(quantity) as num
FROM 
(
SELECT   om.order_id,o.client_ordernum
,max(om.caigou_confirm_status) as caigou_confirm_status_max 
,min(om.caigou_confirm_status) as caigou_confirm_status_min
,max(om.caichuang_can_do) as caichuang_can_do_max
,min(om.caichuang_can_do) as caichuang_can_do_min
,o.caichuang_can_do,o.caichuang_status,o.chejian_status,o.tail_status
,min(om.caigou_confirm_time) as caigou_confirm_time_min
,o.caichuang_can_do_time,o.caichuang_over_time,o.chejian_over_time,o.tail_over_time
,o.quantity
FROM wx_order_material om
LEFT JOIN wx_order o on (om.order_id=o.id)
LEFT JOIN wx_material m on (om.material_id=m.id)
WHERE o.status in (0,1) and o.client_status <> 99 {$sql_where}
and m.type <> 'accessories' 
GROUP BY om.order_id
)  t0
WHERE 1=1
and t0.tail_over_time >= {$param['start_time']}
and t0.tail_over_time <= {$param['end_time']}
GROUP BY sdate";
                        $list = db()->query($sql);
                        $this->temp['tail'] = $list;
                        $mianliao = $this->_ddjd_date_bu($param, 'day_0', $list);
                        return [
                            'tail' => $mianliao,
                        ];
                        break;
                }
                break;
        }
        return $result;
    }

    private function _ddjd_date_bu($param, $type = 'day_0', $data = [])
    {
        switch ($type) {
            case 'day_0':
                $sql = "SELECT  date_format(date,'%Y年%m月%d日') as sdate 
FROM wx_date 
where time >= {$param['start_time']} and time <={$param['end_time']}
order by id asc
";
                $date_array = db()->query($sql);
                $res = [];
                foreach ($date_array as $k => $v) {
                    $temp = [
                        'sdate' => $v['sdate'],
                        'num' => 0,
                    ];
                    foreach ($data as $k3 => $v3) {
                        if ($v3['sdate'] == $v['sdate']) {
                            $temp['num'] = $v3['num'];
                            break;
                        }
                    }
                    $res[] = $temp;
                }
                return $res;
                break;
        }
    }

    private function _ddjd_dealWith($param = [], $data = [])
    {
        $chart = [
//            'chart' => [
//                'type' => 'line',
//            ],
            'title' => [
                'text' => '月平均气温23',
            ],
            'subtitle' => [
                'text' => '数据来源: WorldClimate.com',
            ],
            'xAxis' => [
                'categories' => ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
                //标示线
//                'plotLines' => [[
//                    'color' => 'red',            //线的颜色，定义为红色
//                    'dashStyle' => 'longdashdot',//标示线的样式，默认是solid（实线），这里定义为长虚线
//                    'value' => 3,                //定义在哪个值上显示标示线，这里是在x轴上刻度为3的值处垂直化一条线
//                    'width' => 2                 //标示线的宽度，2px
//                ]],
            ],
            'yAxis' => [
                'title' => [
                    'text' => '件数'
                ],
                'plotLines' => [//平均线处理
                    [
                        'value' => 122,
                        'dashStyle' => 'Dash',
                        'width' => 1,
                        'color' => '#ff0000',
                        'label' => [
                            'text' => "平均值：122",
                            'align' => 'left',
                        ],
                        'zIndex' => 2,
                    ],
                ],
            ],
            'plotOptions' => [
                'series' => [
                    'label' => [
                        'connectorAllowed' => false
                    ],
//                    'pointStart' => 2010,
                ],
                'line' => [
                    'dataLabels' => [
                        'enabled' => true,// 开启数据标签
                    ],
//                    'enableMouseTracking' => false, // 关闭鼠标跟踪，对应的提示框、点击事件会失效
                ],
            ],
            'series' => [
                [
                    'name' => '东京',
                    'data' => [7.0, 6.9, 9.5, 14.5, 18.4, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6],
                ],
                [
                    'name' => '伦敦',
                    'data' => [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8],
                ],
            ],
            'credits' => [
                'enabled' => false,//版权信息 关闭
            ],
            'legend' => [
                'layout' => 'vertical',
                'align' => 'right',
                'verticalAlign' => 'middle',
                'borderWidth' => 0,
            ],
            'tooltip' => [
                'pointFormatter' => "",
            ]
        ];
        $chart['title']['text'] = "";
        $chart['subtitle']['text'] = "";
        $fisrt_key = '';
        foreach ($data as $k => $v) {
            $fisrt_key = $k;
            break;
        }
        $chart['xAxis']['categories'] = $this->_get_xAxis_categories($data[$fisrt_key]);
        $chart['series'] = $this->_ddjd_get_series($data, $chart['xAxis']['categories']);
        $avg_num = 0;
        foreach ($data as $k => $v) {
            foreach ($v as $k2 => $v2) {
                $avg_num += $v2['num'];
                switch ($k) {
                    case 'caigou':
                        $chart['title']['text'] = "采购中近7天完成件数";
                        break;
                }
            }
            $avg_num = $avg_num / count($v);
        }
        $chart['yAxis']['plotLines'][0]['value'] = intval($avg_num);
        $chart['yAxis']['plotLines'][0]['label']['text'] = "平均值：" . intval($avg_num);
        return [
            'chart' => $chart,
        ];
    }

    private function _ddjd_get_series($data, $categories)
    {
        $res = [];
        $f_list = [];
        foreach ($data as $k => $v) {
            foreach ($v as $k2 => $v2) {
                $name = '全部件数';
//                switch ($k) {
//                    case 'caigou':
//                        $name = '采购中';
//                        break;
//                }
                $f_list[$name][] = [
                    'sdate' => $v2['sdate'],
                    'num' => $v2['num'],
                ];
            }
        }
        foreach ($f_list as $k => $v) {
            $temp = [
                'name' => $k,
                'data' => [],
            ];
            foreach ($categories as $k2 => $v2) {
                $flag = 0;
                $num = 0;
                foreach ($v as $k3 => $v3) {
                    if ($v2 == $v3['sdate']) {
                        $num = $v3['num'];
                        $flag = 1;
                        break;
                    }
                }
                $temp['data'][] = floatval($num);//必须是数值，不能字符串
            }
            $res[] = $temp;
        }
        return $res;
    }

}